﻿using System.Diagnostics.CodeAnalysis;
using System.Windows.Controls;
using Pfz.DataTypes;
using Pfz.Factoring;

namespace Pfz.WpfControls.ValueControls
{
	/// <summary>
	/// Control that shows a string representation of many types, such as
	/// integers, short-integers, decimals and others. Of course, string
	/// itself is valid.
	/// </summary>
	[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
	[AutoRegisterInControlFactory(typeof(string))]
	internal sealed class StringControl:
		UserControl,
		IValueControl
	{
		private TextBox _textBox = new TextBox();
		
		/// <summary>
		/// Creates a new StringControl instance.
		/// </summary>
		public StringControl()
		{
			Content = _textBox;
			MinWidth = 60;
		}
		
		/// <summary>
		/// Clears the internal textbox.
		/// </summary>
		public void Clear()
		{
			_textBox.Clear();
		}
		
		/// <summary>
		/// Gets or sets a value indicating if this control is read-only.
		/// </summary>
		public bool IsReadOnly
		{
			get
			{
				return _textBox.IsReadOnly;
			}
			set
			{
				_textBox.IsReadOnly = value;
			}
		}
	
		/// <summary>
		/// Gets or sets the value as a simple string.
		/// </summary>
		public string Value
		{
			get
			{
				string text = _textBox.Text;
				if (text.Length == 0)
					return null;
					
				return text;
			}
			set
			{
				_textBox.Text = value;
			}
		}
		
		#region IValueContainer Members
			object IValueContainer.Value
			{
				get
				{
					return Value;
				}
				set
				{
					Value = (string)value;
				}
			}
		#endregion
	}
}
